package com.css.fxfzfxqh.modules.reportmanagement.controller;

import com.css.fxfzfxqh.base.attachment.entity.AttachmentInfoEntity;
import com.css.fxfzfxqh.base.attachment.service.AttachmentInfoService;
import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.modules.buildings.definitiveEvaluation.resistEarthquakesDE.repository.ResistEarthquakesDERepository;
import com.css.fxfzfxqh.modules.buildings.probabilityEvaluation.resistEarthquakesPE.repository.ResistEarthquakesPERepository;
import com.css.fxfzfxqh.modules.earthquakeRiskAssessmentZoningReport.model.dto.FxqhAssessReportTemplateUpdateDto;
import com.css.fxfzfxqh.modules.reportmanagement.service.ExportReportDataService;
import com.css.fxfzfxqh.modules.reportmanagement.service.ReportDataService;
import com.css.fxfzfxqh.modules.reportmanagement.vo.BuildingVO;
import com.css.fxfzfxqh.modules.reportmanagement.vo.CalculationTasksVO;
import com.css.fxfzfxqh.modules.reportmanagement.vo.TaskReportParamVO;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import com.css.fxfzfxqh.util.PlatformPageUtils;
import com.css.fxfzfxqh.util.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 人口数据
 */
@RestController
@Slf4j
@RequestMapping("/report")
public class ReportController {

    @Autowired
    private ExportReportDataService exportReportDataService;

    @Autowired
    private ReportDataService reportDataService;

    @Autowired
    AttachmentInfoService attachmentInfoService;

    @Resource
    ResistEarthquakesDERepository resistEarthquakesDERepository;

    @Resource
    ResistEarthquakesPERepository resistEarthquakesPERepository;
    /**
     * 生成Word报告统一入口
     * @param vo
     * @return
     */
    @PostMapping("/generateWordReportData")
    public RestResponse<?> generateWordReportData(@RequestBody TaskReportParamVO vo) {
        try {
            if (StringUtils.isBlank(vo.getTaskId())||StringUtils.isBlank(vo.getDataId())) {
                return RestResponse.fail("入参任务ID或者报告ID为空!");
            }
            String typeId = vo.getTypeId();
            String taskId = vo.getTaskId();
            String bgId = vo.getDataId();
            Map<String,Object> taskObj = reportDataService.getTaskObj(taskId);
            //评估方式 0为确定评估1为概率评估
//            String assessType = taskObj.get("assess_type").toString();
//            String assessName = taskObj.get("assess_name").toString();
//            if("0".equals(assessType)){
//                //确定评估
//                Integer countByAssessId = resistEarthquakesDERepository.getCountByAssessId(taskId);
//                if(countByAssessId<1){
//                    return RestResponse.fail("生成失败,"+assessName+"没有进行过评估计算");
//                }
//            }else if("1".equals(assessType)){
//                //概率评估
//                Integer countByAssessId = resistEarthquakesPERepository.getCountByAssessId(taskId);
//                if(countByAssessId<1){
//                    return RestResponse.fail("生成失败,"+assessName+"没有进行过评估计算");
//                }
//            }
            //地震输入方式0为设定烈度1为四个概率,2为地震影响场,  3为发生概率危险性,4为超越概率危险性,多个数字之间用,分隔
            if("0".equals(typeId)){
                exportReportDataService.generateWordReportData(taskId,bgId);
            }else if("1".equals(typeId)){
                exportReportDataService.conditionWordReportData(taskId,bgId);
            }else if("2".equals(typeId)){
                exportReportDataService.earthquakeEffectsWordReportData(taskId,bgId);
            }else if("3".equals(typeId) || "4".equals(typeId)){
                exportReportDataService.probabilityWordReportData1(taskId,bgId,typeId);
            }
            return RestResponse.succeed("生成成功!");
        } catch (Exception e) {
            log.error("save error:{}", e.getMessage(), e);
            return RestResponse.fail("生成失败!");
        }
    }

    @PostMapping("/updateTaskReportData")
    public RestResponse<?> updateTaskReportData(@RequestBody TaskReportParamVO vo) {
        try {
            if (StringUtils.isBlank(vo.getDataId())) {
                return RestResponse.fail("报告ID为空!");
            }
            String dataId = vo.getDataId();
            String finalReportFileId=vo.getFinalReportFileId();
            //String finalReportFileName=vo.getFinalReportFileName();

            AttachmentInfoEntity attach= attachmentInfoService.getAttach(finalReportFileId);
            String finalReportFileName=null;
            if(attach!=null){
                finalReportFileName=attach.getAttachName();
            }
            reportDataService.updateTaskReportData(dataId,finalReportFileId,finalReportFileName);
            return RestResponse.succeed("终版上传成功!");
        } catch (Exception e) {
            log.error("save error:{}", e.getMessage(), e);
            return RestResponse.fail("终版上传失败!");
        }
    }
    /**
     * 生成报告-确定评估-四个概率水准
     * @return
     */
    @PostMapping("/buildConditionWordReportData")
    public RestResponse<?> buildConditionWordReportData(String taskId,String bgId) {
        try {
            if (StringUtils.isBlank(taskId)||StringUtils.isBlank(bgId)) {
                return RestResponse.fail("入参任务ID或者报告ID为空!");
            }
            exportReportDataService.conditionWordReportData(taskId,bgId);
            return RestResponse.succeed("生成成功!");
        } catch (Exception e) {
            log.error("save error:{}", e.getMessage(), e);
            return RestResponse.fail("生成失败!");
        }
    }

    /**
     * 生成报告-确定评估-设定地震影响场数据生成
     * @type 表示生成报告的类型
     * @return
     */
    @PostMapping("/buildEffectsWordReportData")
    public RestResponse<?> buildEffectsWordReportData(String taskId,String bgId,String type) {
        try {
            if (StringUtils.isBlank(taskId)||StringUtils.isBlank(bgId)) {
                return RestResponse.fail("入参任务ID或者报告ID为空!");
            }
            exportReportDataService.earthquakeEffectsWordReportData(taskId,bgId);
            return RestResponse.succeed("生成成功!");
        } catch (Exception e) {
            log.error("save error:{}", e.getMessage(), e);
            return RestResponse.fail("生成失败!");
        }
    }

    /**
     * 生成报告-确定评估-烈度报告
     * @return
     */
    @PostMapping("/buildGenerateWordReportData")
    public RestResponse<?> buildGenerateWordReportData(String taskId,String bgId) {
        try {
            if (StringUtils.isBlank(taskId)||StringUtils.isBlank(bgId)) {
                return RestResponse.fail("入参任务ID或者报告ID为空!");
            }
            exportReportDataService.generateWordReportData(taskId,bgId);
            return RestResponse.succeed("生成成功!");
        } catch (Exception e) {
            log.error("save error:{}", e.getMessage(), e);
            return RestResponse.fail("生成失败!");
        }
    }

    /**
     * 报告管理-查询列表
     * 分页列表
     *
     * @param param 参数
     * @return page
     */
    @GetMapping("/getPage")
    public RestResponse getPage(HttpServletRequest request, BuildingVO param) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String, Object> page = reportDataService.getPage1(curPage, pageSize, param);
            restResponse = RestResponse.succeed(page);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 查询当前任务生成评估报告
     * @return
     */
    @GetMapping("/getReportData")
    public RestResponse getReportData(HttpServletRequest request,String taskId) {
        RestResponse response = null;
        try{
            List<Map<String,Object>> reportList = reportDataService.getTaskReportList(taskId);
            Map<String,Object> taskObj = reportDataService.getTaskObj(taskId);
            Map<String,Object> result = new HashMap<>();
            result.put("reportList",reportList);
            result.put("task",taskObj);
            response = RestResponse.succeed(result);
            response.setMessage("查询成功");
        }catch (Exception e){
            log.error("查询失败!",e);
            response=RestResponse.fail("查询失败!");
        }
        return response;
    }

    public static void main(String[] args) {
        System.out.println(UUIDGenerator.getUUID());
    }
}
